 <?php 
            $labels=sfConfig::get("app_var_label");
            $floats=array("kW","W/mq","kWh","kVarh","°C","m/s","V","A");
            $dataRaw=$sf_data->getRaw("data");
            
            foreach($dataRaw as $plant=>$plantData){
                foreach($plantData as $dev=>$devData){ 
//                    var_dump($devData);die;
                     if (count($devData,COUNT_NORMAL)>0) { ?>
                        <table id="lista" class="floatingTab">
                            <thead>
                                <tr>
                                    <th class="order" colspan= <?php echo count(reset($devData),COUNT_NORMAL)?>><?php echo $plant ?></th>
                                </tr>
                                <tr>
                        <?php      foreach(reset($devData) as $varName=>$val){
                                        if ($varName == 'id') continue;
                                        if ($varName == 'plant') continue;
                                        if ($varName == 'impen') continue;

                                        //replace vars with the configured labels
                                        if (key_exists($varName, $labels))
                                            $label=$labels[$varName];
                                        else
                                            $label=$varName;

                                        if (strpos($label, "_")!== FALSE){
                                            $labelName="<br>".substr($label,0, strpos($label, "_"));
                                            $um=" [".substr($label,strpos($label, "_")+1)."]";
                                        }else{
                                            $labelName="<br>".$label;
                                            $um=NULL;
                                        }

                                        ?>       
                                            <th><?php echo $dev.$labelName.$um?></th>
                             <?php } ?>
                                </tr>
                            </thead>
                            <tbody>
                  <?php         $odd="odd";
                                foreach($devData as $row){
                                    $odd = ($odd=="odd")?'even' : 'odd';?>
                                    <tr class="<?php echo $odd ?>">
                    <?php              foreach($row as $varName=>$val){ 
                        
                                            if ($varName == 'id') continue;
                                            if ($varName == 'plant') continue;
                                            if ($varName == 'impen') continue;

                                            //replace vars with the configured labels
                                            if (key_exists($varName, $labels))
                                                $label=$labels[$varName];
                                            else
                                                $label=$varName;
                                            
                                            //Substitutes foreign key
                                            if ((strtolower($varName) == 'subplant') ){
                                                if (isset($val))
                                                    $val=  Doctrine_Core::getTable("Subplant")->find($val)->getName();
                                                else
                                                    $val="/";
                                            }
                                            
                                            //Insert Measure Units
                                            if (strpos($label, "_")!== FALSE){
                                                $um=substr($label,strpos($label, "_")+1);
                                                if (array_search($um, $floats)!==FALSE)
                                                  $val=  number_format ($val, 2, ".", " " );     
                                            }

                                            ?>
                                                <td><?php echo $val;?></td>
                    <?php              } ?>
                                    </tr>
                    <?php       } ?>              
                            </tbody>
                        </table>
               <?php }else{ ?>
                        <div class="redlight"> Query on <?php echo $dev?> returned no data </div>
            <?php    }?>      
    <?php       } 
            }?> 
